/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sismol.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.*;
import sismol.cartography.Linha;
import sismol.cartography.Ponto;
import sismol.cartography.Trecho;
import sismol.data.GerenciadorCrud;
import sismol.data.GerenciadorFactory;

/**
 *
 * @author Koji
 */
public class GMapsAction extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String op = request.getParameter("op");
        if(op != null) {
            GerenciadorCrud gerenciador = GerenciadorFactory.getGerenciadorCrud();

            String linhaNome = request.getParameter("linha-nome");

            String pontosLatitudeString = request.getParameter("ponto-latitude");
            String[] pontosLatitude = pontosLatitudeString.split(":");
            String pontosLongitudeString = request.getParameter("ponto-longitude");
            String[] pontosLongitude = pontosLongitudeString.split(":");
            String pontosNomesString = request.getParameter("ponto-nome");
            String[] pontosNome = pontosNomesString.split(":");
            String pontosParadaString = request.getParameter("ponto-parada");
            String[] pontosParada = pontosParadaString.split(":");
            String trechosDistanciasString = request.getParameter("trecho-distancia");
            String[] trechosDistancias = trechosDistanciasString.split(":");

            Integer numeroOnibus = Integer.valueOf(request.getParameter("numero-onibus"));

            List<Trecho> trechos = new ArrayList<Trecho>();
            for (int i = 1; i < pontosLongitude.length; i++) {
                String longitudeA = pontosLongitude[i-1];
                String latitudeA = pontosLatitude[i-1];
                Boolean paradaA = Boolean.valueOf(pontosParada[i-1]);

                if(!longitudeA.equals("")){
                    Ponto pontoA = new Ponto();
                    pontoA.setLatitude(Double.valueOf(latitudeA));
                    pontoA.setLongitude(Double.valueOf(longitudeA));
                    pontoA.setNome(pontosNome[i-1]);
                    pontoA.setParada(paradaA);

                    String latitudeB = pontosLatitude[i];
                    String longitudeB = pontosLongitude[i];
                    Boolean paradaB = Boolean.valueOf(pontosParada[i]);

                    Ponto pontoB = new Ponto();
                    pontoB.setLatitude(Double.valueOf(latitudeB));
                    pontoB.setLongitude(Double.valueOf(longitudeB));
                    pontoB.setNome(pontosNome[i]);
                    pontoB.setParada(paradaB);

                    Trecho trecho = new Trecho();
                    trecho.setPontoA(pontoA);
                    trecho.setPontoB(pontoB);
                    trecho.setDistancia(Long.valueOf(trechosDistancias[i-1]));

                    trechos.add(trecho);
                }
            }

            Linha linha = new Linha();
            linha.setNome(linhaNome);
            linha.setTrechos(trechos);

            gerenciador.criarLinhaCompleta(linha, numeroOnibus);

            PrintWriter writer = response.getWriter();

            JSONObject root = new JSONObject();
            try{
            root.put("result", true);
            } catch(JSONException e){
                //nada.
            }
            writer.print(root);
        } else {
            RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/gmaps.jsp");
            rd.forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="MÃƒÂ©todos HttpServlet. Clique no sinal de + ÃƒÂ  esquerda para editar o cÃƒÂ³digo.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
